local skel = fk.CreateSkill {
  name = "emo__heyu",
}

Fk:loadTranslationTable{
  ["emo__heyu"] = "荷语",
  [":emo__heyu"] = "每回合限一次，每当一张牌对你生效时，你可以选择一张手牌视为此牌，且你可以将此牌交给一名其他角色，并摸一张牌。",

  ["#emo__heyu-card"] = "荷语：你可以选择一张手牌，视为【%arg】",
  ["#emo__heyu-give"] = "荷语：你可以将 %arg 交给一名其他角色，并摸一张牌",
  ["@@emo__heyu"] = "荷语",
}

skel:addEffect(fk.CardEffecting, {
  anim_type = "drawcard",
  times = function (_, player)
    return 1 - player:usedSkillTimes(skel.name)
  end,
  can_trigger = function (self, event, target, player, data)
    return data.to == player and player:hasSkill(skel.name) and not player:isKongcheng()
    and player:usedSkillTimes(skel.name) == 0
  end,
  on_cost = function (self, event, target, player, data)
    local cards = player.room:askToCards(player, {
      min_num = 1, max_num = 1, skill_name = skel.name, include_equip = false,
      prompt = "#emo__heyu-card:::" .. data.card.name
    })
    if #cards > 0 then
      event:setCostData(self, {cards = cards})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local cid = event:getCostData(self).cards[1]
    room:setCardMark(Fk:getCardById(cid), "@@emo__heyu", data.card.name)
    Fk:filterCard(cid, player)
    local tos = player.room:askToChoosePlayers(player, {
      min_num = 1, max_num = 1, targets = room:getOtherPlayers(player, false), skill_name = skel.name,
      prompt = "#emo__heyu-give:::" .. Fk:getCardById(cid):toLogString()
    })
    if #tos > 0 then
      room:obtainCard(tos[1], cid, false, fk.ReasonGive, player, skel.name)
      if not player.dead then
        player:drawCards(1, skel.name)
      end
    end
  end,
})

skel:addEffect("filter", {
  card_filter = function (self, card, player)
    return card and type(card:getMark("@@emo__heyu")) == "string"
  end,
  view_as = function (self, player, card)
    local c = Fk:cloneCard(card:getMark("@@emo__heyu"), card.suit, card.number)
    c.skillName = skel.name
    return c
  end,
})

--- 进入非处理区和非手牌区时清除标记，还原原卡
skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    return player.seat == 1
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if move.toArea ~= Card.Processing and move.toArea ~= Player.Hand then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId):getMark("@@emo__heyu") ~= 0 then
            player.room:setCardMark(Fk:getCardById(info.cardId), "@@emo__heyu", 0)
            Fk:filterCard(info.cardId, player.room:getCardOwner(info.cardId))
          end
        end
      end
    end
  end,
})

return skel
